var $table = $("#equipmentTab");
$(function () {
    initTable();
    //条件查询
    $("#queryequipmentBtn").click(function () {
        $table.bootstrapTable('refresh');
    });
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../ep/getAllEquipmentPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "设备编号",
                field: "epDeviceNo",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "设备Mac地址",
                field: "epMac",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "设备所属公司编号",
                field: "epComnum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "设备所属公司名称",
                field: "epComName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "设备token",
                field: "epToken",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "安装时间",
                field: "epInstallTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "更新时间",
                field: "epUpdateTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "GPS位置",
                field: "epGps",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "设备状态",
                field: "epStatus",
                align: 'center',
                formatter: function(value, row, index){
            		var epStatus = "";
            		if(row.epStatus == 1){
            			epStatus = '<span style="color:#ed5565;">正常</span>';
            		}else if(row.epStatus == 2){
            			epStatus ='<span style="color:#23c6c8;">故障</span>';
            		}else if(row.epStatus == 3){
            			epStatus ='<span style="color:#f8ac59;">断开连接</span>';
                    }
            		return epStatus;
            	}
            },
            {
                title: "是否禁用",
                field: "epIsDelete",   
                align: 'center',
                formatter: function(value, row, index){
                    var epIsDelete = "";
                    if(row.epIsDelete == 1){
                        epIsDelete = '<span style="color:#ed5565;">已禁用</span>';
                    }else if(row.epIsDelete == 0){
                        epIsDelete ='<span style="color:#23c6c8;">未禁用</span>';
                    }
                    return epIsDelete;
                }
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var d = '';
                    if (row.epIsDelete == 0){
                        d = '<button class="btn btn-sm btn-danger" type="button" onclick="deleteEquipment(\'' + row.epId + '\',\'' + row.epDeviceNo + '\',\'' + 1 + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
                    }else if(row.epIsDelete == 1){
                        d = '<button class="btn btn-sm btn-warning" type="button" onclick="deleteEquipment(\'' + row.epId + '\',\'' + row.epDeviceNo + '\',\'' + 0 + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
                    }
                    return d;
                }
            }
        ],
        onLoadSuccess: function(data){  //加载成功时执行  
	    	if(data.status == 0 && data.rows.length > 0){
	    		 var array = data.rows;
	    		 isViewColumn = array[0].whetherViewColumn;
	    		 if(isViewColumn == false){	//是否显示列，true显示，false隐藏	
	    			 $table.bootstrapTable('hideColumn', 'epMac');
	    			 $table.bootstrapTable('hideColumn', 'epComnum');
	    			 $table.bootstrapTable('hideColumn', 'epToken');
	    			 $table.bootstrapTable('hideColumn', 'epUpdateTime');
	    		 }
	    	}
        }
    });
}

//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		epDeviceNo: $.trim($('#epDeviceNo').val()),
		epComName: $.trim($('#epComName').val()),
		epStatus: $.trim($('#epStatus').val()),
		epIsDelete: $.trim($('#epIsDelete').val()),
	};
	return temp;
}

//设置终端设备状态
function deleteEquipment(epId, epDeviceNo, isDelete) {
    layer.confirm('你确定要修改【' + epDeviceNo + '】设备状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../ep/modifyEquipmentDisable.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'epId': epId,'isDel':isDelete},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../ep/getAllEquipmentPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}